#!/bin/bash
# 确保脚本被正确调用
if [ $# -ne 1 ]; then
    echo "Usage: ./rename.sh <detector_name>"
    exit 1
fi

# 获取传入的参数，作为探测器名字
DETECTOR_NAME=$1
DETECTOR_NAME_CAPITALIZED=$(echo "$DETECTOR_NAME" | sed 's/\b\(.\)/\u\1/g')   # 首字母大写
DETECTOR_NAME_UPPER=$(echo "$DETECTOR_NAME" | tr '[:lower:]' '[:upper:]')    # 全部大写
DETECTOR_NAME_LOWER=$(echo "$DETECTOR_NAME" | tr '[:upper:]' '[:lower:]')    # 全部小写

# 自动查找所有目录（从当前目录开始）和文件
find_files() {
    # 查找所有的 .cxx, .h 和 CMakeLists.txt 文件
    find . -type f \( -name "*.cxx" -or -name "*.h" -or -name "CMakeLists.txt" -or -name "*.h" \)
}

# 替换文件内容中的NewDetector、NEWDETECTOR 和 newdetector
rename_files() {
    find_files | while read FILE; do
        # echo "Processing file: $FILE"
        # 替换内容
        sed -i -e "s/NewDetector/$DETECTOR_NAME_CAPITALIZED/g" "$FILE"
        sed -i -e "s/NEWDETECTOR/$DETECTOR_NAME_UPPER/g" "$FILE"
        sed -i -e "s/newdetector/$DETECTOR_NAME_LOWER/g" "$FILE"
    done
}

# 修改文件名中的NewDetector
rename_file_names() {
    find_files | while read FILE; do
        # echo "Renaming file: $FILE"
        # 只对文件名进行重命名，替换 NewDetector 为首字母大写的名称
        NEW_NAME=$(echo "$FILE" | sed "s/NewDetector/$DETECTOR_NAME_CAPITALIZED/g")
        if [ "$FILE" != "$NEW_NAME" ]; then
            mv "$FILE" "$NEW_NAME"
        fi
    done
}

# 执行文件和目录中的替换操作
rename_files

# 执行文件重命名操作
rename_file_names

# 输出操作完成信息
echo "Renaming process completed for $DETECTOR_NAME."
echo "Now please rename the directory to $DETECTOR_NAME_LOWER."
